package com.j4dream.property.web;

import com.google.common.collect.ImmutableMap;
import com.j4dream.property.config.PropertiesConfig;
import com.j4dream.property.dao.GeoDao;
import com.j4dream.property.dao.PostDao;
import com.j4dream.property.dao.UserDao;
import com.j4dream.property.model.UserDto;
import com.j4dream.property.service.EmailService;
import com.j4dream.property.web.exception.DuplicateResourceException;
import com.j4dream.property.web.user.CreateUserVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.http.ResponseEntity.ok;

@RestController()
@RequestMapping("/api")
@Slf4j
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private GeoDao geoDao;

    @Autowired
    private PostDao postDao;

    @Autowired
    private EmailService emailService;

    @Autowired
    private PropertiesConfig.EmailConfig emailConfig;

    @ApiOperation("/api")
    @PostMapping("/user")
    public CreateUserVO createUser(@RequestBody @Valid CreateUserVO user, Authentication authentication) {
        if (userDao.existEmail(user.getEmail())) {
            throw new DuplicateResourceException("Email Exist.");
        }
        UserDto userDto = new UserDto();
        userDto.setEmail(user.getEmail());
        userDto.setName(user.getName());
        userDto.setFirstName(user.getFirstName());
        userDto.setLastName(user.getLastName());
        userDto.setCreateTime(System.currentTimeMillis());
        userDao.insertUser(userDto);
        emailService.sendActiveEmail();
        return user;
    }

    @GetMapping("/users")
    public List<UserDto> getUserList() {
        return userDao.getUserList();
    }

    @GetMapping("/currentUser")
    public UserDto getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.info(authentication.getName());
        UserDto userDto = userDao.findOneByEmail(authentication.getName());
        UserDto.Geographic geographic = new UserDto.Geographic();
        geographic.setCity(geoDao.getGeoForKeyLable(userDto.getCity()));
        geographic.setProvince(geoDao.getGeoForKeyLable(userDto.getProvince()));
        userDto.setGeographic(geographic);
        return userDto;
    }

    @PutMapping("/user/enable/{id}")
    public ResponseEntity<?> enableUser(@PathVariable int id) {
        userDao.enableUserById(id);
        return ok("ok");
    }

    @PutMapping("/user/disable/{id}")
    public ResponseEntity<?> disableUser(@PathVariable int id) {
        userDao.disableUserById(id);
        return ok("ok");
    }

    @PostMapping("/user/active")
    public ResponseEntity<?> sendActiveEmail() {
        //emailService.sendMail("");
        return ok(ImmutableMap.of("status", "success","message","xx"));
    }

    @DeleteMapping("/user/{userId}")
    public ResponseEntity<?> delUser(@PathVariable int userId) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDto userDto = (UserDto) authentication.getPrincipal();
        if (userDto.getId() == userId) {
            return ok(ImmutableMap.of("error", "不能删除自己"));
        }
        userDao.deleteUserById(userId);
        return ok("ok");
    }

    @GetMapping("/notices")
    public List getNoticeList() {
        return new ArrayList<>();
    }

    @GetMapping("/project/notice")
    public List getProjecList() {
        return new ArrayList<>();
    }

    @GetMapping("/fake_list")
    public List getPostList() {
        return postDao.getPostList();
    }

}
